<?
/**
* db.php
*
* @author  John Snook <jsnook@gmail.com>
* @package QuickData
* @subpackage includes
*/

/**
* This class provides an abstract interface to the php postgres API
* It is also a factory class for the Query object.
* @todo finalize as abstract, and subclass with mysql, odbc, oracle etc.
* @name DBConnection
* @author  John Snook <jsnook@gmail.com>
* @package QuickData
*/
class DBConnection{
	var $ConnectionID = 0;
	var $ConnectionString = "";
	var $_schemaName;
	
	function DBConnection($ConnectionString = "", $Schema = 'public')
	{
		$this->ConnectionString = $ConnectionString;
		$this->ConnectionID = pg_pconnect($ConnectionString) or user_error( '****' . $ConnectionString);
		$stat = pg_connection_status($this->ConnectionID);
		if ($stat === PGSQL_CONNECTION_OK) {
		} else {
			echo "Couldn't Connect \n$ConnectionString\n" . pg_last_error();
		}   
		$this->setSchema($Schema);
	}
	
	function CreateQuery($SQL)
	{
		if ($this->ConnectionID == 0 && $this->ConnectionString != '')
		{
			$this->DBConnection($this->ConnectionString);
		}
		return new Query($SQL, $this->ConnectionID);
	}
	
	function Close()
	{
		@pg_close($this->ConnectionID);
	}
	
	function LastID($sequence_name)
	{
		//return @pg_last_oid($this->Result);
		$SQL = "SELECT currval('$sequence_name')";
		$query = new Query($SQL, $this->ConnectionID);
		$row = $query->FetchRow(PGSQL_NUM); 
		return $row[0];
	}

	function getSchema()
	{
		return $this->_schemaName;
	}
	
	function setSchema($Name)
	{
		//if ($Name != $this->_schemaName)
		{
			$this->_schemaName = $Name;
			if(headers_sent())
			{
				//echo "\n<!-- SET search_path = $Name -->\n";
			}
			pg_query($this->ConnectionID, "SET search_path = $Name, pg_catalog") or trigger_error($this, E_USER_ERROR);
		}
	}

	function __destruct()
	{
		$this->Close();
	}
}

/**
* This class provides an abstract interface to the php result based postgres API
* @todo finalize as abstract, and subclass with mysql, odbc, oracle etc.
* @todo write wrapper methods for all API functions
* @name Query
* @author  John Snook <jsnook@gmail.com>
* @package QuickData
*/
class Query
{
	var $Result = 0;
	var $SQL;
	var $Errors;
	var $ConnectionID;
	
	/**
	* Query Class Contructor: Executes query in $SQL using $ConnectionID
	* @name Query
	* @param string $SQL
	* @param integer $ConnectionID An open data base resource id.
	*/
	function Query($SQL, $ConnectionID)
	{
		$this->SQL = $SQL;
		$this->ConnectionID = $ConnectionID;
		$this->Result = pg_query($ConnectionID, $SQL);
		if(headers_sent())
		{
			//echo "\n<!-- $SQL -->\n";
		}
		if(!$this->Result)
		{
			echo("Error: $SQL<BR> " . pg_last_error($ConnectionID));
		}
	}

	function __destruct()
	{
		$this->Close();
	}
	
	/**
	* Returns rows affected by update insert or delet 
	* @name AffectedRows
	* @uses function pg_affected_rows 
	*/
	function AffectedRows()
	{
		return pg_affected_rows($this->Result);
	}

	/**
	* Closes the resource
	* @name Close
	* @uses function pg_free_result 
	*/
	function Close()
	{
		@pg_free_result($this->Result);
	}
	
	/**
	* Returns a php data object
	* @name FetchObject
	* @uses function pg_fetch_object 
	*/
	function FetchObject()
	{
		if ($this->Result > 0)
		{
			return pg_fetch_object($this->Result);
		}
	}
	
	/**
	* Returns a php data row array
	* @name FetchRow
	* @uses function pg_fetch_array 
	*/
	function FetchRow($type = PGSQL_ASSOC)
	{
		if ($this->Result > 0)
		{
			return pg_fetch_array($this->Result, NULL, $type);
		}
	}
	
	/**
	* Returns number of rows in the result
	* @name NumRows
	* @uses function pg_num_rows 
	*/
	function NumRows()
	{
		return pg_num_rows($this->Result);
	}
	
}
/*
abstract class AbstractQuery
{
	var $Result = 0;
	var $SQL;
	var $Errors;
	var $ConnectionID;
	function Query($SQL, $ConnectionID)
	{
	}

	function __destruct()
	{
	}
	
	function AffectedRows()
	{
	}

	function Close()
	{
	}
	
	function FetchObject()
	{
	}
	
	function FetchRow()
	{
	}
	
	function NumRows()
	{
	}
	
}
*/
?>